package com.kpx.app.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.ListView;

import com.andview.refreshview.XRefreshView;
import com.kpx.app.R;
import com.kpx.app.base.ActivityBase;
import com.kpx.app.http.HttpDataApi;
import com.kpx.app.http.HttpDataLoader;
import com.kpx.app.http.HttpDataReqUrl;
import com.kpx.app.bean.AddressBean;
import com.kpx.app.adapter.AddressAdapter;
import com.kpx.app.http.HttpDataRequest;
import com.kpx.app.http.ResObj;
import com.kpx.app.utils.MyHashMap;
import com.kpx.app.utils.UIUtils;
import com.kpx.app.utils.UserInfoUtils;
import com.kpx.app.utils.uiutils.IntentUtil;
import com.kpx.app.utils.uiutils.ToastUtil;
import com.kpx.app.view.TitleView;

import java.util.ArrayList;

/**
 * User: 陈聪(617909447@qq.com)
 * Date: 2016-06-24
 * Time: 09:59
 * 订单选择地址页面
 */
public class ActivityOrderChoiceAddress extends ActivityBase {
    public static final int ADDRESS_CHANGE_REQUEST = 1;
    /**
     * 标题栏
     */
    private TitleView title_view;
    private XRefreshView outView;
    private ListView lv_address_show;
    private ArrayList<AddressBean> addressBeans = new ArrayList<>();
    private AddressAdapter adapter;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        UIUtils.setNotifyColor(this, R.color.title_color_grey_sm);
        setContentView(R.layout.activity_super_market_choice_address);
        initTitle();
        initToView();
        setData();
    }

    @Override
    public void initview(@Nullable Bundle savedInstanceState) {

    }

    @Override
    public void initData() {

    }

    @Override
    public void onReceiver(Context context, Intent intent) {

    }

    private void initTitle() {
        title_view = (TitleView) findViewById(R.id.titleview);
        UIUtils.setNormalTitle(title_view,getResources().getString(R.string.title_choice_address));
        title_view.setTxtSize(0, 12);
        title_view.setRight("新增", new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ActivityOrderAddAddress.type = 0;
                IntentUtil.gotoActivityForResult(ActivityOrderChoiceAddress.this, ActivityOrderAddAddress.class, ADDRESS_CHANGE_REQUEST);
            }
        }, R.color.white);
    }

    /**
     * 设置数据
     */
    private void setData() {
        getAddress();// 获取数据
        adapter = new AddressAdapter(ActivityOrderChoiceAddress.this, addressBeans, R.layout.item_super_market_choice_address);
        lv_address_show.setAdapter(adapter);
    }

    /**
     * 初始化相关view
     */
    private void initToView() {
        outView = (XRefreshView) findViewById(R.id.custom_view);
        lv_address_show = (ListView) findViewById(R.id.lv_address_show);
        initRefreshView();
        //商品列表的上下拉刷新操作
        outView.setXRefreshViewListener(new XRefreshView.SimpleXRefreshListener() {
            @Override
            public void onRefresh() {
                getAddress();// 获取数据
            }

            @Override
            public void onLoadMore(boolean isSlience) {
                outView.setLoadComplete(true);
            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
        addressBeans.clear();
        adapter.setData(addressBeans);
        getAddress();// 获取数据
    }

    private void getAddress() {
        showLoadingDialog();
        UserInfoUtils.getAddress(ActivityOrderChoiceAddress.this, new UserInfoUtils.OnGetAddressdataBackListener() {
            @Override
            public void back(ArrayList<AddressBean> aBeans) {
                dissmissLoadingDialog();
                outView.stopRefresh();
                if (aBeans != null) {
                    if (aBeans.size() == 0) {
                        ToastUtil.getInstance().showToast(ActivityOrderChoiceAddress.this, "您还未添加收货地址！");
                    } else {
                        addressBeans.clear();
                        addressBeans.addAll(aBeans);
                        adapter.setData(addressBeans);
                    }
                } else {
                    ToastUtil.getInstance().showToast(ActivityOrderChoiceAddress.this, "获取数据失败");
                }
            }
        });
    }


    /**
     * 初始化上下拉刷新
     */
    private void initRefreshView() {
        outView.setPullLoadEnable(true);
        outView.setPinnedTime(1000);
        outView.setAutoLoadMore(true);
//		outView.setCustomHeaderView(new CustomHeader(this));
//		outView.setCustomHeaderView(new XRefreshViewHeader(this));
        outView.setMoveForHorizontal(true);
//        outView.setCustomFooterView(new CustomerFooter(this));
//		outView.setPinnedContent(true);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode) {
            case ADDRESS_CHANGE_REQUEST:
                getAddress();
                break;
        }
    }
}
